using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using LocalBoard.Domain;
using LocalBoard.Controller;
using LocalBoard.Web.UserControls;

namespace LocalBoard.Web
{
	/// <summary>
	/// Summary description for Admin.
	/// </summary>
	public partial class Admin : System.Web.UI.Page
	{

		private readonly AdminController _adminController = new AdminController(Global.AccessorFactory);

		protected void Page_Load(object sender, System.EventArgs e)
		{
			if(!User.IsInRole(UserRole.ADMIN_ROLE_NAME))
				Security.RedirectToHome();
			if(!IsPostBack)
			{
				clubList.DataSource = _adminController.GetAllClubList();
				clubList.DataTextField = "Name";
				clubList.DataValueField = "Id";
				InitializeClubCategoryList();
				InitializeClubUserList();
				DataBind();
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

		private int GetIntFromString(string value)
		{
			if(value == null || value.Length == 0)
				return 0;
			return Convert.ToInt32(value);
		}

		protected void clubList_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			clubInformation.ClubId.Text = clubList.SelectedValue;
			clubInformation.InitializeClubInformation();
		}

		protected void clubCategoryList_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			clubCategoryName.Text = clubCategoryList.SelectedItem.Text;
		}

		protected void create_Click(object sender, System.EventArgs e)
		{
			if(clubCategoryName.Text.Length > 0)
			{
				_adminController.CreateClubCategory(clubCategoryName.Text);
				InitializeClubCategoryList();
			}
		}

		protected void update_Click(object sender, System.EventArgs e)
		{
			if(clubCategoryList.SelectedIndex > 0 && clubCategoryName.Text.Length > 0
				&& clubCategoryList.SelectedItem.Text != clubCategoryName.Text)
			{
				_adminController.UpdateClubCategory(clubCategoryName.Text, Convert.ToInt32(clubCategoryList.SelectedValue));
				InitializeClubCategoryList();
			}
		}

		protected void delete_Click(object sender, System.EventArgs e)
		{
			if(clubCategoryList.SelectedIndex > 0)
			{
				_adminController.DeleteClubCategory(Convert.ToInt32(clubCategoryList.SelectedValue));
				InitializeClubCategoryList();
			}
		}

		protected void userList_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			Profile user = _adminController.GetUser(Convert.ToInt32(userList.SelectedValue));
			userName.Text = user.UserName;
			InitializeSelectedUserRoles(user.Id);
			InitializeUserRoleList(user.Id);
		}

		private void InitializeClubCategoryList()
		{
			clubCategoryList.Items.Clear();
			foreach(DictionaryEntry item in _adminController.GetAllClubCategories())
			{
				clubCategoryList.Items.Add(new ListItem(item.Value.ToString(), item.Key.ToString()));
			}
			//			if(clubCategory.Items.Count > 0)
			//				clubCategory.SelectedIndex = 1;
		}

		private void InitializeClubUserList()
		{
			userList.Items.Clear();
			foreach(Profile profile in _adminController.GetAllUserList())
			{
				userList.Items.Add(new ListItem(profile.UserId, profile.Id.ToString()));
			}
		}

		private void InitializeUserRoleList(int profileId)
		{
			ArrayList assignedRoles = _adminController.GetUserRolesAssignedToUser(profileId);
			allUserRoles.Items.Clear();
			foreach(DictionaryEntry item in _adminController.GetAllUserRoles())
			{
				if(!IsRoleIdAssigned(assignedRoles, Convert.ToInt32(item.Key)))
					allUserRoles.Items.Add(new ListItem(((UserRole)item.Value).Name, item.Key.ToString()));
			}
		}

		private void InitializeSelectedUserRoles(int profileId)
		{
			selectedUserRoles.Items.Clear();
			foreach(UserRole role in _adminController.GetUserRolesAssignedToUser(profileId))
			{
				selectedUserRoles.Items.Add(new ListItem(role.Name.ToString(), role.Id.ToString()));
			}
		}

		protected void deleteRole_Click(object sender, System.EventArgs e)
		{
			if(selectedUserRoles.SelectedIndex >= 0)
			{
				ListItem item = selectedUserRoles.Items[selectedUserRoles.SelectedIndex];
				allUserRoles.Items.Add(item);
				selectedUserRoles.Items.RemoveAt(selectedUserRoles.SelectedIndex);
			}
		}

		protected void addRole_Click(object sender, System.EventArgs e)
		{
			if(allUserRoles.SelectedIndex >= 0)
			{
				ListItem item = allUserRoles.Items[allUserRoles.SelectedIndex];
				selectedUserRoles.Items.Add(item);
				allUserRoles.Items.RemoveAt(allUserRoles.SelectedIndex);
			}
		}

		private bool IsRoleIdAssigned(ArrayList roleList, int roleId)
		{
			foreach(UserRole role in roleList)
			{
				if(role.Id == roleId)
					return true;
			}
			return false;
		}

		protected void updateUserRole_Click(object sender, System.EventArgs e)
		{
			int profileId = Convert.ToInt32(userList.SelectedValue);
			ArrayList assignedRoles = _adminController.GetUserRolesAssignedToUser(profileId);
			//Add new roles
			foreach(ListItem item in selectedUserRoles.Items)
			{
				int roleId = Convert.ToInt32(item.Value);
				if(!IsRoleIdAssigned(assignedRoles, roleId))
				{
					_adminController.AssignUserRoleToUser(roleId, profileId);
				}
			}
			assignedRoles = _adminController.GetUserRolesAssignedToUser(profileId);
			//Delete roles
			foreach(UserRole role in assignedRoles)
			{
				if(selectedUserRoles.Items.FindByValue(role.Id.ToString()) == null)
				{
					_adminController.UnAssignRoleFromUser(role.Id, profileId);
				}
			}
		}
	}
}
